package com.ocm.controller.util;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import org.springframework.context.support.ResourceBundleMessageSource;

import com.ocm.core.constant.OCMConstant;
import com.ocm.core.domain.AccessMask;
import com.ocm.core.domain.PropertyType;
import com.ocm.core.domain.UserDetailType;
import com.ocm.core.dto.GroupDto;
import com.ocm.core.dto.PropertyDefinitionDto;
import com.ocm.core.dto.PropertyValueDto;
import com.ocm.core.dto.SecurityDto;
import com.ocm.core.dto.UserInfoDto;


public class OCMUtil {

   public static boolean hasIDNotExist(Long id) {
      if (id == null || id == 0)
         return true;
      return false;
   }

   public static List<PropertyTypeModel> listPropertyTypes(ResourceBundleMessageSource messageSource) {
      List<PropertyTypeModel> propertyTypes = new ArrayList<PropertyTypeModel>();
      propertyTypes.add(new PropertyTypeModel(PropertyType.STRING.getPropertyType(), messageSource.getMessage(
            "property.propertyType.String", null, null)));
      propertyTypes.add(new PropertyTypeModel(PropertyType.INTEGER.getPropertyType(), messageSource.getMessage(
            "property.propertyType.Integer", null, null)));
      propertyTypes.add(new PropertyTypeModel(PropertyType.DOUBLE.getPropertyType(), messageSource.getMessage(
            "property.propertyType.Double", null, null)));
      propertyTypes.add(new PropertyTypeModel(PropertyType.DATE.getPropertyType(), messageSource.getMessage(
            "property.propertyType.Date", null, null)));
      propertyTypes.add(new PropertyTypeModel(PropertyType.BOOLEAN.getPropertyType(), messageSource.getMessage(
            "property.propertyType.Boolean", null, null)));
      propertyTypes.add(new PropertyTypeModel(PropertyType.LONG.getPropertyType(), messageSource.getMessage(
            "property.propertyType.Long", null, null)));
      propertyTypes.add(new PropertyTypeModel(PropertyType.INPUTSTREAM.getPropertyType(), messageSource.getMessage(
            "property.propertyType.InputStream", null, null)));
      propertyTypes.add(new PropertyTypeModel(PropertyType.OBJECT.getPropertyType(), messageSource.getMessage(
            "property.propertyType.Object", null, null)));
      return propertyTypes;
   }

   public static Long[] convertIDsFromJsonString(String jsonProperties){
      Long[] ids = null;
      if (jsonProperties.trim().length() > 1){
         JSONArray jsonArray = JSONArray.fromObject(jsonProperties);
         ids = new Long[jsonArray.size()];
         for (int index=0; index<ids.length; index++){
            ids[index] = jsonArray.getLong(index);
         }
      }
      if (ids == null){
         ids = new Long[0];
      }
      return ids;
  }

   public static int[] convertFromJsonString(String jsonProperties){
      int[] ids = null;
      if (jsonProperties.trim().length() > 1){
         JSONArray jsonArray = JSONArray.fromObject(jsonProperties);
         ids = new int[jsonArray.size()];
         for (int index=0; index<ids.length; index++){
            ids[index] = jsonArray.getInt(index);
         }
      }
      
      if (ids == null){
         ids = new int[0];
      }
      return ids;
  }

   public static List<PropertyDefinitionDto> getRestProperties(List<PropertyDefinitionDto> properties, List<PropertyDefinitionDto> removeProperies){
      if (removeProperies != null){
         for (int index=0; index<removeProperies.size(); index++){
            PropertyDefinitionDto propertyDefinitionDto = removeProperies.get(index); 
            if (properties.contains(propertyDefinitionDto)){
               properties.remove(propertyDefinitionDto);
            }
         }
      }
      return properties;
  }

   public static List<PropertyDefinitionDto> getEditabledProperties(boolean editable, List<PropertyDefinitionDto> properties, String... includedProperties){
      List<PropertyDefinitionDto> editableProperties = new ArrayList<PropertyDefinitionDto>();
      if (properties != null){
         for (int index=0; index<properties.size(); index++){
            PropertyDefinitionDto propertyDefinitionDto = properties.get(index); 
            if (propertyDefinitionDto.getEditable() == editable || existedIncludedProperties(propertyDefinitionDto.getName(), includedProperties)){
               editableProperties.add(propertyDefinitionDto);
            }
         }
      }
      return editableProperties;
  }

   public static boolean existedIncludedProperties(String actual, String... includedProperties){
      if (includedProperties != null && includedProperties.length > 0){
         for (String expected : includedProperties){
            if (expected.equals(actual)){
               return true;
            }
         }
      }
      return false;
   }
   public static PropertyValueDto getPropertyValue(String name, List<PropertyValueDto> properties) {
      for (PropertyValueDto property : properties) {
         if (property.getProperty().getName().equalsIgnoreCase(name)){
            return property;
         }
      }
      return null;
   }

   public static List<GroupDto> getRestGroups(List<GroupDto> allGroups, List<GroupDto> assignedGroups){
      if (assignedGroups != null){
         for (int index=0; index<assignedGroups.size(); index++){
            GroupDto groupDto = assignedGroups.get(index); 
            if (allGroups.contains(groupDto)){
               allGroups.remove(groupDto);
            }
         }
      }
      return allGroups;
  }
   
   public static List<SecurityDto> getDefaultSecurities(List<UserInfoDto> userInfoDtos){
      List<SecurityDto> securityDtos = new ArrayList<SecurityDto>();
      for (UserInfoDto userInfoDto : userInfoDtos){
         SecurityDto securityDto = new SecurityDto();
         securityDto.setHasCreator(false);
         securityDto.setHasUser((userInfoDto.getType()==UserDetailType.USER.getType()?true:false));
         securityDto.setHasGroup((userInfoDto.getType()==UserDetailType.GROUP.getType()?true:false));
         securityDto.setHasAnonymous((OCMConstant.SEC_ANONYMOUS_GROUP.equalsIgnoreCase(userInfoDto.getUsername())?true:false));
         securityDto.setAccessMask(AccessMask.VIEWPROPERTIES.getAccessMask());
         securityDto.setUserId(userInfoDto.getId());
         securityDto.setUserInfo(userInfoDto);
         securityDtos.add(securityDto);
      }
      return securityDtos;
  }
}
